# Copyright Materialize, Inc. and contributors. All rights reserved.
#
# Use of this software is governed by the Business Source License
# included in the LICENSE file at the root of this repository.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0.
from textwrap import dedent
from typing import List

from materialize.checks.actions import Testdrive
from materialize.checks.checks import Check


class NumericTypes(Check):
    def initialize(self) -> Testdrive:
        return Testdrive(
            dedent(
                """
            > CREATE TABLE numeric_types_table (smallint_col SMALLINT, integer_col INTEGER, bigint_col BIGINT, numeric_col NUMERIC(39,16));
            > INSERT INTO numeric_types_table DEFAULT VALUES;
            > INSERT INTO numeric_types_table VALUES (-32768, -2147483648, -9223372036854775808, -12345678901234567890123.4567890123456789);
            > INSERT INTO numeric_types_table VALUES (32767, 2147483647, 9223372036854775807, 12345678901234567890123.4567890123456789);
        """
            )
        )

    def manipulate(self) -> List[Testdrive]:
        return [
            Testdrive(dedent(s))
            for s in [
                """
                > CREATE MATERIALIZED VIEW numeric_types_view1 AS
                  SELECT
                  smallint_col, -32768::smallint AS smallint2, 32767::smallint AS smallint3,
                  integer_col, -2147483648::integer AS integer2, 2147483647::integer AS integer3,
                  bigint_col, -9223372036854775808::bigint AS bigint2, 9223372036854775807::bigint AS bigint3,
                  numeric_col, -12345678901234567890123.4567890123456789 AS numeric2, 12345678901234567890123.4567890123456789::NUMERIC(39,16) AS numeric3
                  FROM numeric_types_table
                  WHERE smallint_col >= -32768 AND smallint_col <= 32767
                  AND integer_col >= -2147483648 AND integer_col <= 2147483647
                  AND bigint_col >= -9223372036854775808 AND bigint_col <= 9223372036854775807
                  AND numeric_col >= -12345678901234567890123.4567890123456789 AND numeric_col <= 12345678901234567890123.4567890123456789
                  OR smallint_col IS NULL;

                > INSERT INTO numeric_types_table SELECT * FROM numeric_types_table;
                """,
                """
                > CREATE MATERIALIZED VIEW numeric_types_view2 AS
                  SELECT
                  smallint_col, -32768::smallint AS smallint2, 32767::smallint AS smallint3,
                  integer_col, -2147483648::integer AS integer2, 2147483647::integer AS integer3,
                  bigint_col, -9223372036854775808::bigint AS bigint2, 9223372036854775807::bigint AS bigint3,
                  numeric_col, -12345678901234567890123.4567890123456789 AS numeric2, 12345678901234567890123.4567890123456789::NUMERIC(39,16) AS numeric3
                  FROM numeric_types_table
                  WHERE smallint_col >= -32768 AND smallint_col <= 32767
                  AND integer_col >= -2147483648 AND integer_col <= 2147483647
                  AND bigint_col >= -9223372036854775808 AND bigint_col <= 9223372036854775807
                  AND numeric_col >= -12345678901234567890123.4567890123456789 AND numeric_col <= 12345678901234567890123.4567890123456789
                  OR smallint_col IS NULL;
                > INSERT INTO numeric_types_table SELECT * FROM numeric_types_table;
                """,
            ]
        ]

    def validate(self) -> Testdrive:
        return Testdrive(
            dedent(
                """
                > SELECT * FROM numeric_types_view1;
                -32768 -32768 32767 -2147483648 -2147483648 2147483647 -9223372036854775808 -9223372036854775808 9223372036854775807 -12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                -32768 -32768 32767 -2147483648 -2147483648 2147483647 -9223372036854775808 -9223372036854775808 9223372036854775807 -12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                -32768 -32768 32767 -2147483648 -2147483648 2147483647 -9223372036854775808 -9223372036854775808 9223372036854775807 -12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                -32768 -32768 32767 -2147483648 -2147483648 2147483647 -9223372036854775808 -9223372036854775808 9223372036854775807 -12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                32767 -32768 32767 2147483647 -2147483648 2147483647 9223372036854775807 -9223372036854775808 9223372036854775807 12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                32767 -32768 32767 2147483647 -2147483648 2147483647 9223372036854775807 -9223372036854775808 9223372036854775807 12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                32767 -32768 32767 2147483647 -2147483648 2147483647 9223372036854775807 -9223372036854775808 9223372036854775807 12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                32767 -32768 32767 2147483647 -2147483648 2147483647 9223372036854775807 -9223372036854775808 9223372036854775807 12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                <null> -32768 32767 <null> -2147483648 2147483647 <null> -9223372036854775808 9223372036854775807 <null> -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                <null> -32768 32767 <null> -2147483648 2147483647 <null> -9223372036854775808 9223372036854775807 <null> -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                <null> -32768 32767 <null> -2147483648 2147483647 <null> -9223372036854775808 9223372036854775807 <null> -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                <null> -32768 32767 <null> -2147483648 2147483647 <null> -9223372036854775808 9223372036854775807 <null> -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789

                > SELECT * FROM numeric_types_view2;
                -32768 -32768 32767 -2147483648 -2147483648 2147483647 -9223372036854775808 -9223372036854775808 9223372036854775807 -12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                -32768 -32768 32767 -2147483648 -2147483648 2147483647 -9223372036854775808 -9223372036854775808 9223372036854775807 -12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                -32768 -32768 32767 -2147483648 -2147483648 2147483647 -9223372036854775808 -9223372036854775808 9223372036854775807 -12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                -32768 -32768 32767 -2147483648 -2147483648 2147483647 -9223372036854775808 -9223372036854775808 9223372036854775807 -12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                32767 -32768 32767 2147483647 -2147483648 2147483647 9223372036854775807 -9223372036854775808 9223372036854775807 12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                32767 -32768 32767 2147483647 -2147483648 2147483647 9223372036854775807 -9223372036854775808 9223372036854775807 12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                32767 -32768 32767 2147483647 -2147483648 2147483647 9223372036854775807 -9223372036854775808 9223372036854775807 12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                32767 -32768 32767 2147483647 -2147483648 2147483647 9223372036854775807 -9223372036854775808 9223372036854775807 12345678901234567890123.4567890123456789 -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                <null> -32768 32767 <null> -2147483648 2147483647 <null> -9223372036854775808 9223372036854775807 <null> -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                <null> -32768 32767 <null> -2147483648 2147483647 <null> -9223372036854775808 9223372036854775807 <null> -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                <null> -32768 32767 <null> -2147483648 2147483647 <null> -9223372036854775808 9223372036854775807 <null> -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
                <null> -32768 32767 <null> -2147483648 2147483647 <null> -9223372036854775808 9223372036854775807 <null> -12345678901234567890123.4567890123456789 12345678901234567890123.4567890123456789
            """
            )
        )
